clear
clear mata
clear matrix

cd ..                                                                            /* Goes back to parent folder */

capture log close
log using "log\09_Committees.log", replace

mkdir "tmp"                                                                      /* creates a temporary folder to store temp files */

*       Patriotism!

*       FIRST VERSION  June       7, 2022
*       THIS VERSION   June       7, 2022
*       LAST RUN       June       7, 2022

*       LAST REVISOR		BC, LMC

*       Log of revisions:

*       This prepares committees data

********************************************************************************
****                      PLAN OF THE PROCEDURE                             ****
****  1. Stewart database: Committee membership                             ****
****  2. ICPSR database: tenure information                                 ****
****  3. Construct tenure on the agricultural database at county level      ****
****  4. Finalize database                                                  ****
****  5. Erase junk                                                         ****
********************************************************************************

********************************************************************************
****  1. Stewart database: Committee membership                             ****
********************************************************************************
do "code-build/01_Shopping_List.do"

import delimited using "rawdata/Stewart/congress_committee_3879.dat", clear delim(tab) 

gen CONGRESS  = substr(v1,  1, 2)
gen SESSION   = substr(v1,  3, 1)
gen COMM      = substr(v1,  4, 3)
gen ICPSR     = substr(v1,  7, 5)
gen NAME      = substr(v1, 12,25)
gen PTYSTAT   = substr(v1, 37, 1)
gen RANK      = substr(v1, 38, 2)
gen YEAR      = substr(v1, 44, 4)
gen YEAR_END  = substr(v1, 52, 4) 
gen PARTY     = substr(v1, 56, 4)
compress NAME

drop v1
bysort CONGRESS ICPSR: gen n = _n
destring CONGRESS COMM ICPSR PTYSTAT RANK YEAR YEAR_END PARTY , replace 
drop if ICPSR == 0
reshape wide COMM NAME PTYSTAT RANK YEAR YEAR_END PARTY, i(CONGRESS ICPSR) j(n)  
								 /* *THE SAME PERSON CAN BE PART OF VARIOUS COMMITTEES. WE RESHAPE THE DATA: THE OBSERVATIONAL UNIT IS NOW REP PER CONGRESS */

drop NAME2 NAME3 NAME4 NAME5 NAME6 NAME7 NAME8 SESSION
destring CONGRESS, replace force /* data error is the only non numeric that is lost 
									LEVEL OF OBSERVATION IS NOW CONGRESS ICPSR. */
									
									
save "tmp/committees", replace

********************************************************************************
****  2. ICPSR database: tenure information                                 ****
********************************************************************************
import delimited using "rawdata/ICPSR/07803-0001-Data.dat" , clear stringcols(_all)

rename ïv1  MEMBER_ID 
rename v2   CONGRESS
rename v3   CHAMBER
rename v4   STATE 
rename v5   DISTRICT 
rename v6   PARTY 
rename v7   PARTY2 
rename v8   NAME
rename v41  AGE_FIRST_CONGRESS
rename v42  FIRST_CONGRESS
rename v46  FIRST_YEAR_CONGRESS
rename v47  LAST_YEAR_CONGRESS
rename v96  CUM_TENURE
rename v99  CUM_TERM_HOUSE
drop v*
compress NAME

tostring               FIRST_YEAR_CONGRESS LAST_YEAR_CONGRESS CHAMBER, replace
foreach var of varlist FIRST_YEAR_CONGRESS LAST_YEAR_CONGRESS{
	replace `var' = "1" + `var'
	destring `var', replace
} /* foreach var */

rename   MEMBER_ID ICPSR
destring ICPSR CONGRESS, replace
destring PARTY2        , replace

drop if DISTRICT == "99"

drop if FIRST_YEAR_CONGRESS < 1900
drop if FIRST_YEAR_CONGRESS > 1950
compress

drop if CHAMBER != "3"   /*	this keeps only the house */
keep if CONGRESS == 62 | CONGRESS == 73


merge 1:1 CONGRESS ICPSR using "tmp/committees", keep(3) nogen /* 101 ICPSR CONGRESS OBS IN  MASTER ARE NOT MATCHED; 6,4k are*/ 

sort ICPSR CONGRESS

********************************************************************************
****  3. Construct tenure on the agricultural database at county level      ****
********************************************************************************
gen AGRICULTURE      = 0 
gen AGRICULTURE_T    = 0

destring CUM_TENURE CUM_TERM_HOUSE, replace
foreach var of varlist COMM1 COMM2 COMM3 COMM4 COMM5 COMM6 COMM7 COMM8 {
	replace AGRICULTURE      = 1              if `var' ==   2
	local   num = substr("`var'", -1,1) 
	replace AGRICULTURE_T    = CUM_TERM_HOUSE if `var' ==   2 
} /* foreach var */

keep ICPSR CONGRESS STATE DISTRICT AGRICULTURE*
keep if CONGRESS == 62 | CONGRESS == 73

rename STATE stateicpsr                                                             /* DATASET IS AT CONGRESS DISTRICT LEVEL */

collapse (sum)  AGRICULTURE AGRICULTURE_T, by(stateicpsr DISTRICT CONGRESS)
destring stateicpsr DISTRICT, replace

foreach i in 62 73 {	 /*	MERGE TO CONGRESS SPECIFIC BRIDGE  */
	preserve 
		keep if CONGRESS == `i'
		
		merge 1:m stateicpsr DISTRICT using "data/Bridge-ND-DISTRICT_ID`i'", nogen  /* 49 obs from using cant be merged */
		
		tostring stateicpsr, gen(STATEN_STR)            /* EACH OBS IS NOW A COUNTY IN CONGRESS i */
		tostring DISTRICT  , gen(DIST_STR) 
		gen DISTRICT_ID = STATEN_STR + "_" + DIST_STR 	/* GENERATE DISTRICT ID */
		
		bysort stateicpsr countynd: egen x = max(WEIGHT) 
		replace DISTRICT_ID = "" if x != WEIGHT         /* ONLY KEEP DISTRICT ID FOR DISTRICT THAT CONTRIBUTES MOST */
		
		foreach var of varlist AGRI* { /* USE WEIGHTS */
			replace `var' =       `var' * WEIGHT
			replace `var' = round(`var',  0.1)
			ren     `var'         `var'`i'
		} /* foreach var */
		ren DISTRICT_ID DISTRICT_ID`i'

		foreach v of var * {
			local l`v' : variable label `v'
			if `"`l`v''"' == "" {
				local l`v' "`v'"
			} /* if lv = "" */
		} /* foreach v */

		collapse (firstnm) DISTRICT_ID (sum) AGRI* , by(stateicpsr countynd)
		foreach v of var * {
			label var `v' "`l`v''"
		} /* foreach v */
		
		keep AGRI* stateicpsr countynd DISTRICT_ID
		drop if countynd == .
		save "tmp/congress`i'", replace
	restore
} /* forvalues i */

********************************************************************************
****  4. Finalize database                                                  ****
********************************************************************************
use "tmp/congress62", replace

merge 1:1 countynd stateicpsr using "tmp/congress73", nogen

egen ID62 = group(DISTRICT_ID62) 
egen ID73 = group(DISTRICT_ID73)

keep stateicpsr countynd ID62 ID73 AGRICULTURE_T62 AGRICULTURE_T73

ren ID62 DISTRICT_ID62
ren ID73 DISTRICT_ID73

lab var DISTRICT_ID62 		    "Electoral district in 1911-13 (62nd Congress)"
lab var DISTRICT_ID73 		    "Electoral district in 1933-35 (73rd Congress)"
lab var AGRICULTURE_T73 		"Tenure in Agriculture Committee 1933-35 (Stewart)"
lab var AGRICULTURE_T62 		"Tenure in Agriculture Committee  1911-13 (Stewart)"

save "data/committees", replace

********************************************************************************
****  5. Erase junk                                                         ****
********************************************************************************
erase "tmp/committees.dta"
erase "tmp/congress62.dta"
erase "tmp/congress73.dta"

rmdir "tmp/"

log close
exit